package com.chezhujie.www.controller;

import com.chezhujie.www.core.page.Pagination;
import com.chezhujie.www.domain.Czjorder;
import com.chezhujie.www.service.CzjorderService;
import com.chezhujie.www.service.CzjusecouponService;
import com.chezhujie.www.utils.ConvertObj;
import com.chezhujie.www.utils.UUIDUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *   订单表
 * Created by 汤伟杰 on 2017/5/23.
 */
@Controller
@RequestMapping("/czjorder")
public class CzjorderController {
    @Autowired
    private CzjorderService czjorderService;
    @Autowired
    private CzjusecouponService czjusecouponService;

    /** 列表 **/
    @RequestMapping("/list.do")
    public String list(Integer pageNo, Integer pageSize, Czjorder czjorder, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjorder,req,model);
//        setUserinfoId(model);
        setBusinessId(model);
        Map<String, Object> params = ConvertObj.toMap(czjorder);
        params.put("nickname", req.getParameter("nickname"));
        params.put("phone", req.getParameter("phone"));
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 20;
        }
        try {
        /*	List<Integer> pays=czjorderService.getPays();*/
        		int pay1=czjorderService.getPay1();//未完成
        		int pay2=czjorderService.getPay2();//已完成
        		model.addAttribute("pay1", pay1);
            	model.addAttribute("pay2", pay2);
            	
        		


            Pagination pagination = czjorderService.list(pageNo.intValue(), pageSize.intValue(), params, model);
            model.addAttribute("pagination", pagination);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed","获取订单表列表失败!"+e.getMessage());
        }
        model.addAttribute("entity", params);
        return "czjorder/list";
    }
    /** 保存 **/
    @RequestMapping("/save.do")
    public String save(Czjorder czjorder,
                      HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        try {

            if(StringUtils.isNotBlank(czjorder.getId())){
                czjorderService.update(czjorder);


            }else{
                czjorder.setId(UUIDUtils.getUUID());
                czjorderService.insert(czjorder);
            }
            model.addAttribute("success", "保存订单表成功");
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "保存订单表失败!详情："+e.getMessage());
        }
        return "redirect:list.do";
    }


    /** 详情 **/
    @RequestMapping("/detail.do")
    public String detail(@RequestParam(required = true)String orderCode,Czjorder czjorder, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjorder,req,model);
        try {
            Map<String,Object> entity = czjorderService.queryDetailById(orderCode);
            String couponsId = null;
            if(entity.get("couponsId")!=null){
                couponsId = String.valueOf(entity.get("couponsId"));
            }
            czjusecouponService.setCouponsByOrderCode(orderCode,couponsId,model);

            model.addAttribute("entity", entity);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "查询订单表出错!详情："+e.getMessage());
        }
        return "czjorder/detail";
    }
    /** 新增或修改 **/
    @RequestMapping("/add.do")
    public String add(Czjorder czjorder, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjorder,req,model);
        setUserinfoId(model);
        setBusinessId(model);
        if(StringUtils.isNotBlank(czjorder.getId())){
            czjorder = czjorderService.queryById(czjorder.getId());
            model.addAttribute("entity", czjorder);
        }
        return "czjorder/add";
    }
    /** 删除 **/
    @RequestMapping("/delete.do")
    public String delete(@RequestParam(required = true)String id,Czjorder czjorder, HttpServletRequest req, HttpServletResponse resp,
                     ModelMap model){
        try {
            czjorderService.deleteById(czjorder.getId());
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", e.getMessage());
        }
        return "redirect:list.do";
    }
    private void setUserinfoId(ModelMap model) {
        Map<String,Object> param = new HashMap<String,Object>();
        List<Map<String,Object>> listUserinfoId = czjorderService.getListUserinfoId(param);
        // map.put("name","");
        // map.put("value","");
        model.addAttribute("listUserinfoId", listUserinfoId);
    }
    private void setBusinessId(ModelMap model) {
        Map<String,Object> param = new HashMap<String,Object>();
        List<Map<String,Object>> listBusinessId = czjorderService.getListBusinessId(param);
        // map.put("name","");
        // map.put("value","");
        model.addAttribute("listBusinessId", listBusinessId);
    }

    private void setMsg(Czjorder czjorder,HttpServletRequest req, ModelMap model) {
        model.addAttribute("failed", req.getParameter("failed"));
        model.addAttribute("success", req.getParameter("success"));
        model.addAttribute("entity", czjorder);
    }

    /** 订单汇总信息 **/
    @RequestMapping("/summary.do")
    public String summary(Integer pageNo, Integer pageSize,Czjorder czjorder, HttpServletRequest req, HttpServletResponse resp,
                         ModelMap model){
        try {
            if (pageNo == null) {
                pageNo = 1;
            }
            if (pageSize == null) {
                pageSize = 20;
            }
            Pagination pagination = czjorderService.summarylist(pageNo.intValue(), pageSize.intValue(), new HashMap<String, Object>(), model);

            model.addAttribute("pagination", pagination);

        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", e.getMessage());
        }
        return "czjorder/summary";
    }
    
    ////////////////////////////订单接口//////////////////////////
    
}
